<?php
$loader->requireOnce('ordo/Insurance.class.php');

class C_MedicalPrintView extends Controller {
	private	$hospitalInfo;
	
    function C_MedicalPrintView ($template_mod = "general") {
        parent::Controller();
        $this->_db = $GLOBALS['frame']['adodb']['db'];
    	if (Celini::getCurrentController() == 'MedicalPrintView') {
			$menu =& Menu::getInstance();
			$menu->currentSection = 'patient';
		}
        $this->template_mod = $template_mod;
        $this->view->path = 'print_patient';
        
       	$hospitalInfo = Celini::configInstance()->get('hospital_info');
       	$this->hospitalInfo	=	$hospitalInfo[L10nHelper::getLocale()];
    }

	public function actionPrintMedicalRecord($patientId=0) {
		if (isset($_GET['patientId'])) {
			$patientId = $_GET['patientId'];
		}

    	$this->assign_by_ref('person', Celini::newORDO('Person', $patientId));
		$this->assign_by_ref('patient', Celini::newORDO('Patient', $patientId));
    	$this->assign_by_ref('insurance', Insurance::fromPatientId($patientId));
    	$this->assign_by_ref("patientIssues", ORDataObject::factory('PatientMedicalAgent')->getPatientIssues($patientId));
    	$this->assign_by_ref("patientAllergies", ORDataObject::factory('PatientAllergy')->getPatientAllergies($patientId));
    	$this->assign_by_ref("patientChronics", ORDataObject::factory('PatientChronicDisease')->getPatientChronics($patientId));
    	$this->assign_by_ref("patientOtherDiseases", ORDataObject::factory('PatientOtherDisease')->getPatientOtherDisease($patientId));
    	$this->assign_by_ref("patientSurgeries", ORDataObject::factory('PatientSurgery')->getPatientSergeries($patientId));

    	return $this->view->render("print_record.html");
	}

	public function actionPrintForPatient() {

	}

	public function actionCheckPrescription(){
		$ticketItem = Celini::newORDO('TicketItem', $_GET['ticketItemId']);
		$ticketId = $ticketItem->get('ticket_id');

		// Get record from ticket
		$record = Celini::newORDO('MedicalRecord');
		$recordId = $record->getRecordIdFromTicket($ticketId);
		if ($recordId == false) {
			return L10nHelper::_("The patient does not have examination info");
		}
		// Get current prescription_id of this patient
		$pres = Celini::newORDO('Prescription');
		$prescriptionId = $pres->getPrescriptionIdFromRecord($recordId);
		if ($prescriptionId == false) {
			return L10nHelper::_("The patient does not have a prescription");
		}
		return "OK";
	}

	public function actionCheckPrescription2() {
		$recordId = $_GET['recordId'];
		// Get current prescription_id of this patient
		$pres = Celini::newORDO('Prescription');
		$prescriptionId = $pres->getPrescriptionIdFromRecord($recordId);
		if ($prescriptionId == false) {
			return L10nHelper::_("The patient does not have a prescription");
		}
		return "OK";
	}

	public function actionViewPrescriptionPrint($ticketItemId = 0) {
		$ticketItem = Celini::newORDO('TicketItem', $_GET['ticketItemId']);
		$ticketId = $ticketItem->get('ticket_id');
		$patientId = $ticketItem->getPatientFromTicketItem($ticketItemId);
		$this->assign_by_ref('person', Celini::newORDO('Person', $patientId));
		$this->assign_by_ref('patient', Celini::newORDO('Patient', $patientId));
		// Get record from ticket
		$record = Celini::newORDO('MedicalRecord');
		$recordId = $record->getRecordIdFromTicket($ticketId);
		if ($recordId == false) {
			return L10nHelper::_("The patient does not have examination info");
		}

		return $this->printPrescription($recordId, $_GET['lang']);
	}

	public function actionViewPrescriptionPrintFromRecord() {
		$recordId = $_GET['recordId'];
		$r = Celini::newORDO('MedicalRecord', $recordId);
		$ticketId = $r->get('ticket_id');
		$config	=	Celini::configInstance();
		$printSignature	=	$config->get('default_print_signature');
		if(isset($_GET['printSign'])){
			$printSignature	= $_GET['printSign'];
		}
		$printPresPrice	=	$config->get('print_prescription_price');
		$ticket			=	Celini::newORDO('Ticket', $ticketId);
		$patientId 		= 	$ticket->get('patient_id');
		
		$this->assign_by_ref('person', Celini::newORDO('Person', $patientId));
		$this->assign_by_ref('patient', Celini::newORDO('Patient', $patientId));
		$this->assign('printSignature', $printSignature);
		$this->assign('printPresPrice', $printPresPrice);

		return $this->printPrescription($recordId);
	}

	public function actionPrintPrescription() {
		
	    $prescriptionId = $_GET['prescriptionId'];
	    $lang = $_GET['lang'];
	    $bklang = L10nHelper::getLocale();
	    L10nHelper::selectLanguage($lang);
	    
        $prescriptionObj = Celini::newORDO('Prescription');
        $prescription = $prescriptionObj->getPrescription($prescriptionId);
        $patientId = $prescriptionObj->getPatient($prescriptionId);
        $patientObj = Celini::newOrdo('Patient', $patientId);
        
        if (isset($prescription['date'])) {
            $prescription['date_formatted'] = L10nHelper::renderDateTime($prescription['date'], 'short', 'date');
        }
        
        $presItemObj = Celini::newORDO('PrescriptionItem');
        $prescriptionItems = $presItemObj->getPrescriptionItems($prescriptionId);

        foreach ($prescriptionItems as &$item) {
        	$item['usage_title_text'] = Utils::getEnumTextByKey(EnumerationConstant::PHA_DM_DEFAULT_USAGE , $item['usage_title']);
            $item['dosage_title_text']  = Utils::getEnumTextByKey(EnumerationConstant::PHA_IMSE_UNIT , $item['dosage_title']);
            $item['quantity_title_text']  = Utils::getEnumTextByKey(EnumerationConstant::PHA_IMSE_UNIT , $item['quantity_title']);
        }
        $this->assign('prescription', $prescription);
        $this->assign('presItem', $prescriptionItems);
        $this->assign('usageKneadingConst', EnumerationConstant::PHA_DM_DEFAULT_USAGE_KNEADING);
        $this->assign('usageGargleConst', EnumerationConstant::PHA_DM_DEFAULT_USAGE_GARGLE);
        $this->assign_by_ref('person', Celini::newORDO('Person', $patientId));
        $this->assign('patientObj', $patientObj);
        
          
        $config	=	Celini::configInstance();
        $hospitalInfo = $config->get('hospital_info');
        $this->assign('hospitalInfo', $hospitalInfo[$lang]);
		$html = '';
        if (L10nHelper::isSupportedLocale($lang)) {
            $html = $this->view->render('print_prescription_' . $lang . '.html');
        }
        L10nHelper::selectLanguage($bklang);
        return $html;
	}
		
		
	/**
	 * Print 'Death Inside Hospital Report'
	 * @param $deathInfoId
	 * @return page to print
	 */
    public function actionPrintReportDeadInside($deathInfoId = 0){
    	if(isset($_GET['deathInfoId'])){
    		$deathInfoId  =   $_GET['deathInfoId'];
    	}
        $objDeathInfo   =   Celini::newORDO('DeathInsideInfo', $deathInfoId);
        $patientId      =   $objDeathInfo->get('patient_id');
        $objPatient		=	Celini::newORDO('Person', $patientId);
        $patientObj		=	Celini::newORDO('Patient', $patientId);
        $patientCode	=	$patientObj->getPatientCode();
        $departmentId	=	$objDeathInfo->get('department_id');
        $department		=	Celini::newORDO('Department', $departmentId)->getLocalizedText('name');
        
        $this->assign_by_ref('patient', $objPatient);
        $this->assign_by_ref('deathInfo', $objDeathInfo);
        $this->assign_by_ref('patientCode', $patientCode);
        $this->assign_by_ref('department', $department);
        $this->assign_by_ref('hospitalInfo', $this->hospitalInfo);
        
        return $this->view->render('print_report_dead_inside.html');
    }

}
?>
